package org.vectrics.recruiter.user;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.common.db.DataUtils;
import org.vectrics.common.db.RecordObject;
import org.vectrics.common.util.DateSync;
import org.vectrics.company.SalaryType;
import org.vectrics.location.Location;
import org.vectrics.user.Person;
import org.vectrics.user.PersonEducationType;

/**
 * This class stores information about a potential candidate for a job.  The information
 * includes candidate preferences. 
 * @author Mike Moore
 *
 */
public class CandidateInfo implements RecordObject, Serializable {
	
	public static final long serialVersionUID = 932462343;

	private transient final Logger log = Logger.getLogger(this.getClass());
	private Integer travelPercent = null;
	private boolean lookingFullTime = true;
	private boolean lookingPartTime = true;
	private boolean lookingContract = true;
	private boolean lookingPermanent = true;
	private boolean okToContact = true;
	private boolean currentlyWorking = true;
	private boolean badContactInformation = false;
	private boolean active = true;
	private Set jobAgents = new HashSet();
	private Long id = null;
	private Set jobLocations = new HashSet();
	private Set resumes = new HashSet();
	private Location residenceLocation = null;
	private Person person = null;
	private Person source = null;
	private BigDecimal expectedSalary = null;
	private SalaryType salaryType = null;
	private boolean voided = false;
	private Date lastActivityDate = DateSync.getCurrentTime();
	private String careerGoals = null;
	private PersonEducationType highestEducation = null;
	
	
    /**
     * @return Returns the id.
     */
    public Long getId() {
        return id;
    }
    /**
     * @param id The id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }
	/**
	 * Constructor 
	 */
	public CandidateInfo() {
		super();
	}

	/**
	 * @return Returns the active.
	 */
	public boolean getActive() {
		return active;
	}
	/**
	 * @param active The active to set.
	 */
	public void setActive(boolean active) {
		this.active = active;
	}
	public boolean getVoided() {
		return voided;
	}

	public void setVoided(boolean voided) {
		this.voided = voided;
	}

	public boolean getLookingContract() {
		return lookingContract;
	}

	public void setLookingContract(boolean lookingContract) {
		this.lookingContract = lookingContract;
	}


	public boolean getLookingFullTime() {
		return lookingFullTime;
	}

	public void setLookingFullTime(boolean lookingFullTime) {
		this.lookingFullTime = lookingFullTime;
	}

	public boolean getLookingPartTime() {
		return lookingPartTime;
	}

	/**
	 * @param lookingPartTime
	 */
	public void setLookingPartTime(boolean lookingPartTime) {
		this.lookingPartTime = lookingPartTime;
	}

	/**
	 * @return
	 */
	public boolean getLookingPermanent() {
		return lookingPermanent;
	}

	/**
	 * @param lookingPermanent
	 */
	public void setLookingPermanent(boolean lookingPermanent) {
		this.lookingPermanent = lookingPermanent;
	}

	/**
	 * @return
	 */
	public Integer getTravelPercent() {
		return travelPercent;
	}

	/**
	 * @param travelPercent
	 */
	public void setTravelPercent(Integer travelPercent) {
		this.travelPercent = travelPercent;
	}
	
    public JobAgent addJobAgent() {
    	JobAgent jobAgent = new JobAgent();
        getJobAgents().add(jobAgent);
        if (jobAgent.getCandidateInfo() != this)
            jobAgent.setCandidateInfo(this);
        return(jobAgent);
    }
	
	/**
	 * @return Returns the jobAgents.
	 */
	public Set getJobAgents() {
		return jobAgents;
	}

	/**
	 * @param jobAgents The jobAgents to set.
	 */
	public void setJobAgents(Set jobAgents) {
		log.debug("setJobAgents() called");
		this.jobAgents = jobAgents;
		Iterator iter = jobAgents.iterator();
		while (iter.hasNext()) {
			JobAgent jobAgent = (JobAgent)iter.next();
			jobAgent.setCandidateInfo(this);
		}
	}

    /**
     * @return Returns the interviews.
    public Set getInterviews() {
        return interviews;
    }
    public void setInterviews(Set interviews) {
        this.interviews = interviews;
    }
     */

	/**
     * @return Returns the jobLocations.
     */
    public Set getJobLocations() {
        return jobLocations;
    }
    /**
     * @param jobLocations The jobLocations to set.
     */
    public void setJobLocations(Set jobLocations) {
        this.jobLocations = jobLocations;
    }

    public void addJobLocation(Location location) {
    	CandidateLocation candLoc = this.findJobLocation(location);
    	if (candLoc == null) {
			candLoc = new CandidateLocation();
	    	candLoc.setCandidate(this);
	    	candLoc.setLocation(location);
	        this.getJobLocations().add(candLoc);
	        if (candLoc.getCandidate() != this)
	            candLoc.setCandidate(this);
    	}
    }
    
    public CandidateLocation findJobLocation(Location location) {
    	CandidateLocation candidateLocation = null;
    	Iterator iter = this.getJobLocations().iterator();
    	while (iter.hasNext()) {
    		CandidateLocation candLoc = (CandidateLocation)iter.next();
    		if (candLoc.getLocation().getId().equals(location.getId()));
    			candidateLocation = candLoc;
    	}
    	return(candidateLocation);
    }
    
    /**
     * @return Returns the person.
     */
    public Person getPerson() {
        return person;
    }
    /**
     * @param person The person to set.
     */
    public void setPerson(Person person) {
        this.person = person;
    }
    /**
     * @return Returns the residenceLocation.
     */
    public Location getResidenceLocation() {
        return residenceLocation;
    }
    /**
     * @param residenceLocation The residenceLocation to set.
     */
    public void setResidenceLocation(Location residenceLocation) {
        this.residenceLocation = residenceLocation;
    }
    
    
	/**
	 * @return Returns the expectedSalary.
	 */
	public BigDecimal getExpectedSalary() {
		return expectedSalary;
	}
	/**
	 * @param expectedSalary The expectedSalary to set.
	 */
	public void setExpectedSalary(BigDecimal expectedSalary) {
		this.expectedSalary = expectedSalary;
	}
	/**
	 * @return Returns the salaryType.
	 */
	public SalaryType getSalaryType() {
		return salaryType;
	}
	/**
	 * @param salaryType The salaryType to set.
	 */
	public void setSalaryType(SalaryType salaryType) {
		this.salaryType = salaryType;
	}
	/**
	 * @return Returns the source.
	 */
	public Person getSource() {
		return source;
	}
	/**
	 * @param source The source to set.
	 */
	public void setSource(Person source) {
		this.source = source;
	}
	/**
	 * @return Returns the badContactInformation.
	 */
	public boolean getBadContactInformation() {
		return badContactInformation;
	}
	/**
	 * @param badContactInformation The badContactInformation to set.
	 */
	public void setBadContactInformation(boolean badContactInformation) {
		this.badContactInformation = badContactInformation;
	}
	/**
	 * @return Returns the currentlyWorking.
	 */
	public boolean getCurrentlyWorking() {
		return currentlyWorking;
	}
	/**
	 * @param currentlyWorking The currentlyWorking to set.
	 */
	public void setCurrentlyWorking(boolean currentlyWorking) {
		this.currentlyWorking = currentlyWorking;
	}
	/**
	 * @return Returns the okToContact.
	 */
	public boolean getOkToContact() {
		return okToContact;
	}
	/**
	 * @param okToContact The okToContact to set.
	 */
	public void setOkToContact(boolean okToContact) {
		this.okToContact = okToContact;
	}
	
	
	/**
	 * @return Returns the lastActivityDate.
	 */
	public Date getLastActivityDate() {
		return lastActivityDate;
	}
	/**
	 * @param lastActivityDate The lastActivityDate to set.
	 */
	public void setLastActivityDate(Date lastActivityDate) {
		this.lastActivityDate = lastActivityDate;
	}
	
	/**
	 * @return Returns the resumes.
	 */
	public Set getResumes() {
		return resumes;
	}
	/**
	 * @param resumes The resumes to set.
	 */
	public void setResumes(Set resumes) {
		this.resumes = resumes;
	}
	
	public Resume addResume() {
		Resume resume = new Resume();
		this.resumes.add(resume);
		resume.setDomain(this.getPerson().getDomain());
		resume.setCandidateInfo(this);
		return(resume);
	}

	public void removeResume(Resume resume) {
		Iterator iter = this.resumes.iterator();
		while (iter.hasNext()) {
			Resume curResume = (Resume)iter.next();
			if (curResume.getId().equals(resume.getId())) {
				curResume.setVoided(true);
			}
		}
	}

	public Resume getLatestResume() {
		Set nonVoidedResumes = DataUtils.removeVoided(this.resumes);
		Iterator iter = nonVoidedResumes.iterator();
		Resume latestResume = null;
		while (iter.hasNext()) {
			Resume resume = (Resume)iter.next();
			if ((latestResume == null) || (resume.getLastUpdate().after(latestResume.getLastUpdate()))) {
				latestResume = resume;
			}
		}
		return(latestResume);
	}
	
	public String getCareerGoals() {
		return careerGoals;
	}
	
	public void setCareerGoals(String careerGoals) {
		this.careerGoals = careerGoals;
	}
	
	public PersonEducationType getHighestEducation() {
		return highestEducation;
	}
	
	public void setHighestEducation(PersonEducationType highestEducation) {
		this.highestEducation = highestEducation;
	}
	
}
